import re
import time

import requests
import execjs
from bs4 import BeautifulSoup
proxies = {
    'http': 'socks5://127.0.0.1:8442',
    'https': 'socks5://127.0.0.1:8442'
}
with open("demo.js", "r", encoding="utf-8") as f:
    js = f.read()
js_code = execjs.compile(js)

def get_captcha(cid, appid):
    req = js_code.call("get_cap", appid, cid)
    print(req)
    headers = {
        "Accept": "*/*",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Pragma": "no-cache",
        "Referer": "https://exaccount2.eastmoney.com/",
        "Sec-Fetch-Dest": "script",
        "Sec-Fetch-Mode": "no-cors",
        "Sec-Fetch-Site": "same-site",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
        "sec-ch-ua": "\"Chromium\";v=\"139\", \"Not;A=Brand\";v=\"99\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\""
    }
    cookies = {
        "qgqp_b_id": "3a2b824b8728dca7e5a26503a5a1db01",
        "p_origin": "https%3A%2F%2Fpassport2.eastmoney.com",
        "st_nvi": "7zLmZ-Jrv2MCegjzVyUPXa7aa",
        "nid": "0aa81a24ddb440b3de2b44fdcc9ad357",
        "nid_create_time": "1754302061460",
        "gvi": "VyauCXU7GInHyTYFCvDqcafba",
        "gvi_create_time": "1754302061460",
        "st_si": "38104876047078",
        "st_pvi": "59424994565045",
        "st_sp": "2025-08-04%2018%3A07%3A41",
        "st_inirUrl": "https%3A%2F%2Fpassport2.eastmoney.com%2F",
        "st_sn": "1",
        "st_psi": "20250804180740841-0-1849181463",
        "st_asi": "20250804180740841-0-1849181463-grzx.dlym.zhmmdl.dl-1"
    }
    url = "https://smartvcode2.eastmoney.com/Titan/api/captcha/get"
    params = {
        "callback": "cb",
        "ctxid": cid,
        "request": req,
        "_": str(int(time.time()*1000))
    }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)

    print(response.text)
    print(response)
def login4():
    url = "https://exaccount2.eastmoney.com/home/Login4"
    headers = {
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,ar;q=0.7",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "Origin": "https://exaccount2.eastmoney.com",
        "Pragma": "no-cache",
        "Referer": "https://exaccount2.eastmoney.com/home/Login4?rc=550656496",
        "RequestVerificationToken": "di6k8wpeJdmYifzMGkCvdG3UPNkyzkCjhgP2EkhmRQyD8ugFfQcwdNLTfFMafRwvbBfZ8RqxhPpCpq88zL196Ext37E1",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
        "X-Requested-With": "XMLHttpRequest",
        "deviceType": "Web",
        "domainName": "passport2.eastmoney.com",
        "productType": "UserPassport",
        "sec-ch-ua": "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Microsoft Edge\";v=\"138\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\""
    }
    params = {
        "rc": "354855057"
    }
    cookies = {
        "qgqp_b_id": "af4a3d01781b5555726d9a17e425be7a",
        "st_nvi": "2xfzbOLOA9PSvGMSOq0F6856a",
        "nid": "04d51e4d408a5c3c863d17873895a224",
        "nid_create_time": "1754271580335",
        "gvi": "a5R5YyyjtEfOf8i6-mdDl6c36",
        "gvi_create_time": "1754271580335",
        "st_si": "00585701374988",
        "st_asi": "delete",
        "p_origin": "https%3A%2F%2Fpassport2.eastmoney.com",
        "st_pvi": "67053581251557",
        "st_sp": "2025-08-04%2009%3A39%3A40",
        "st_inirUrl": "https%3A%2F%2Fpassport2.eastmoney.com%2F",
        "st_sn": "6",
        "st_psi": "20250804172606150-0-1320210713"
    }
    response = requests.get(url, headers=headers, params=params,cookies=cookies,proxies=proxies)
    html = response.text
    capt_app_id = None
    match = re.search(r"var\s+CaptAppId\s*=\s*'(\d+)';", html)
    if match:
        capt_app_id = match.group(1)
        print("解析到的 CaptAppId:", capt_app_id)
    else:
        print("CaptAppId 没找到")
    soup = BeautifulSoup(html, 'html.parser')
    ctxid = soup.find('input', id='hdAccountCaptContextId')
    if ctxid is None:
        print("ctxid not found")
        return
    print("Login4 status==>", response.status_code)
    print("Login4 ctxid['value']==>", ctxid['value'])
    with open("login4.html", "w", encoding="utf-8") as f:
        f.write(html)
        print("Login4 html saved")
    cid = ctxid['value']
    return cid, capt_app_id
if __name__ == '__main__':
    cid, appid = login4()
    get_captcha(cid, appid)
    get_captcha(cid, appid)